package com.woniu.idearbac.security;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.woniu.idearbac.util.Result;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 功能描述
 *
 * @author PC
 * @date -2022/12/2-时间-14:26
 * @product idea-rbac
 */
@Component
public class JWTLogoutSuccessHandler implements LogoutSuccessHandler {
    @Resource
    private RedisCache redisCache;
    @Override
    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        //删除redis中的用户信息,
        redisCache.deleteObject("rbac:"+loginUser.getUsername());

        response.setContentType("application/json;charset=UTF-8");
        String string = new ObjectMapper().writeValueAsString(Result.ok("退出成功"));

        response.getWriter().println(string);
    }
}
